import * as cc from "cc";
import yyComponent from "../../Script/Common/yyComponent";
import { EventType } from "../../Script/GameSpecial/GameEventType";
import PlayerData from "../../Script/Common/PlayerData";
import GlobalPool from "../../Script/Common/GlobalPool";
import { GlobalEnum } from "../../Script/GameSpecial/GlobalEnum";
import Action3dManager, { ActionMngType } from "../../Script/Common/Action3dManager";

//玩家的金币体力等资产信息条
const { ccclass, property } = cc._decorator;

@ccclass
export default class PlayerAssetBar extends yyComponent {

    public init() {
        this.initGoldIcon();
        this.onEvents();
        this.setData();
    }
    protected onEvents() {
        this.on(EventType.Common.PlayerDataEvent.playerDataChanged, this.onPlayerDataChanged, this);
        this.on(EventType.Common.UIEvent.playGoldAnim, this.playGoldAnim, this);
        this.on(EventType.LevelEvent.changeGoldPos, this.setGoldBarPos, this);
    }
    public reset() {
        this.resetGoldIcon();
    }

    public show() {
        this.node.active = true;
        this.setData();
        this.onEvents();
    }
    public hide() {
        this.offEvents();
        this.node.active = false;
    }

    protected setData() {
        let data = PlayerData.getData("gameData.asset");
        this.setGold(data.gold);
    }

    protected convertToString(v: number) {
        return v.toString();
    }

    protected onPlayerDataChanged(data) {
        switch (data.attribute) {
            case "gameData.asset.gold": {
                this.setGold(data.value);
                break;
            }
        }
    }

    //#region 金币信息
    @property(cc.Label)
    protected goldLabel: cc.Label = null;
    protected gold: number;
    protected addGold(v) {
        this.emit(EventType.Common.AudioEvent.playEffect, GlobalEnum.AudioClip.gold, { volume: 0.5 });
        this.setGold(this.gold + v);
    }
    protected setGold(gold: number) {
        this.gold = gold;
        this.goldLabel.string = this.convertToString(gold);
    }
    //#endregion

    //#region 金币动画
    @property(cc.Node)
    protected goldLayer: cc.Node = null;
    @property(cc.Node)
    protected goldIconTargetNode: cc.Node = null;
    @property(cc.Prefab)
    protected goldIconPrefab: cc.Prefab = null;
    protected cb: Function;
    protected finishGold: number = null;
    protected initGoldIcon() {
        this.cb = null;
        GlobalPool.createPool(this.goldIconPrefab.name, this.goldIconPrefab);
    }
    protected resetGoldIcon() {
        this.cb = null;
        let actMng = Action3dManager.getMng(ActionMngType.UI);
        for (let i = this.goldLayer.children.length - 1; i >= 0; --i) {
            actMng.stopAllActions(this.goldLayer.children[i]);
        }
        GlobalPool.putAllChildren(this.goldLayer, true);
    }
    protected playGoldAnim(data: { startPos?: cc.Vec2, cb?: Function, gold?: number }) {
        // this.finishGold = PlayerData.getData("gameData.asset.gold");
        // if (!!data.gold) {
        //     this.finishGold += data.gold;
        // }

        this.emit(EventType.Common.AudioEvent.playEffect, GlobalEnum.AudioClip.reward);
        this.resetGoldIcon();
        this.emit(EventType.Common.UIEvent.showTouchMask);
        this.cb = data.cb;

        let center = cc.v3();
        if (!!data.startPos) {
            center.x = data.startPos.x;
            center.y = data.startPos.y;
        }

        let duration = 0.6;
        this.goldIconCount = Math.round(Math.random() * 10) + 20;
        let count = this.goldIconCount;
        if (!!data.gold) {
            this.addGoldStep = Math.floor(data.gold / count);
        } else {
            this.addGoldStep = 1;
        }
        let scope = 800;
        let actMng = Action3dManager.getMng(ActionMngType.UI);
        let p = this.goldIconTargetNode.getComponent(cc.UITransform).convertToWorldSpaceAR(cc.v3());
        p = this.node.getComponent(cc.UITransform).convertToNodeSpaceAR(p);
        let d2 = 0.7;
        for (let i = 0; i < count; i++) {
            let item = GlobalPool.get(this.goldIconPrefab.name);
            this.goldLayer.addChild(item);
            item.setPosition(center);
            item.setScale(1, 1);
            let jump = Action3dManager.jumpTo(duration + Math.random() * 0.5, (Math.random() - 0.5) * scope + center.x, Math.random() * 80 + center.y, 0, 3000 + i * 100);
            let move = Action3dManager.moveTo(d2, p.x, p.y, 0);
            move.easing(Action3dManager.easeSinIn());
            let scale = Action3dManager.scaleTo(d2, 0.5, 0.5, 1);
            let spawn = Action3dManager.spawn(move, scale);
            let cb = Action3dManager.callFun(this.onGoldParticleArrived, this, item);
            let seq = Action3dManager.sequence(jump, spawn, cb);
            actMng.runAction(item, seq);
        }
    }

    protected addGoldStep: number = 1;
    protected goldIconCount: number = 0;
    protected onGoldParticleArrived(node?) {
        if (!!node) {
            GlobalPool.put(node);
        }
        this.addGold(this.addGoldStep);
        // this.emit(EventType.Common.AudioEvent.playEffect, GlobalEnum.AudioClip.gold, { volume: 0.5 });
        if (--this.goldIconCount <= 0) {
            this.playFinish();
        }
    }
    protected playFinish() {
        // this.setGold(this.finishGold);
        this.emit(EventType.Common.UIEvent.hideTouchMask);
        if (!!this.cb) {
            this.cb();
        } else {
            this.emit(EventType.Common.UIEvent.goldAnimPlayFinish);
        }
        this.resetGoldIcon();
    }

    //#endregion

    //#region 体力信息

    //#endregion

    //#region 体力动画

    //#endregion

    protected _tmpPos = cc.v3();
    protected setGoldBarPos(left: boolean = true) {
        this.goldIconTargetNode.getComponent(cc.Widget).enabled = false;
        let p = this.goldIconTargetNode.getPosition();
        if (left) {
            p.x = -327;
        } else {
            p.x = -97;
        }

        cc.Tween.stopAllByTarget(this._tmpPos);
        cc.tween(this._tmpPos).to(0.3, { x: p.x }, {
            onUpdate: () => {
                this.goldIconTargetNode.setPosition(this._tmpPos);
            }
        }).start();
    }


    protected onBtnTest() {
        this.playGoldAnim({ gold: 200 });
    }

    protected onBtnGetGold() {
        this.playClickBtn();
        this.showUI(GlobalEnum.UI.getGold);
    }
}
